<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/admin';

    public function __construct()
    {
        $this->middleware(['guest:admin'])->except('logout');
    }
    public function adminlogin()
    {
        return view('admin.login');
    }
    public function username()
    {
        return 'mobile';
    }
    protected function guard()
    {
        return Auth::guard('admin');
    }
    public function logout(Request $request)
    {
        $this->guard()->logout();

        $request->session()->invalidate();

        return redirect('/admin');
    }
    public function item(Request $request)
    {
        $pageNum = $request->pageNum;
        $pageSize = $request->pageSize;
        $keyword = $request->keyword;
        if (empty($pageNum)) { $pageNum = 1;}
        if (empty($pageSize)) {$pageSize = 2;}
        $startNum = ($pageNum - 1) * $pageSize;
        $count = Item::count();
        $query = Item::orderBy('id', 'desc');
        if (!empty($keyword)) {
            $query = $query->where('name', 'like', '%' . $keyword . '%');
            $count = Item::where('name', 'like', '%' . $keyword . '%')->count();}
        $items = $query->take($pageSize)->skip($startNum)->get();
        return response()->json(['totalCount' => $count, 'items' => $items]);
    }
}
